package com.lazar.servlet;

import com.lazar.model.Admin;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Admin admin = new Admin();
        admin.setName(req.getParameter("name"));
        admin.setPass(req.getParameter("pass"));

        try {
            admin.login();
            HttpSession session = req.getSession();
            session.setAttribute("logined","true");
            req.getRequestDispatcher("/list").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
